<?php
/**
 * Created by PhpStorm.
 * User: sykbxc
 * Date: 2019/4/8
 * Time: 19:27
 */

namespace App\Services\AdminService\Services;


use App\Services\AdminService\Models\AdminUserModel as User;
use App\Services\BaseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserService extends BaseService
{

    /**
     * 重写获取用户表格信息
     * @param $query
     * @param $param
     * @return mixed
     * @throws \Exception
     */
    public static function getData($query, $param, $fun = '')
    {
        return parent::getData($query, $param, function ($query) use ($param) {
            if (!empty($param['user_role'])) {
                $query = $query->join('admin_model_has_roles', 'admin_model_has_roles.model_id', '=', 'id')
                    ->where("admin_model_has_roles.role_id", $param['user_role']);
            }
            return $query;
        });
    }


    /**
     * 通过id查询用户
     * @param $id
     * @return mixed
     */
    public static function getByIdOrFail($id)
    {
        return User::findOrFail($id);
    }


    /**
     * 查询数据库所有的管理员
     * @return User[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getAll()
    {
        return User::all();
    }

    public static function checkPassword($password)
    {
        $model = self::getByIdOrFail(Auth::id());
        if ($model && password_verify($password, $model['password'])) {
            return true;
        }
        return false;
    }
}